package com.sdp.irrigation.kafka.biz.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.redis.RedisUtil;
import com.sdp.irrigation.cache.EmpCache;
import com.sdp.irrigation.cache.EqRealTimeCache;
import com.sdp.irrigation.cache.EqValveCache;
import com.sdp.irrigation.cache.IrrigationCacheKeys;
import com.sdp.irrigation.db.irrigation_emp.service.IIrrigationEmpService;
import com.sdp.irrigation.db.irrigation_eq_log_user_status.service.IIrrigationEqLogUserStatusService;
import com.sdp.irrigation.db.irrigation_eq_order.service.IIrrigationEqOrderService;
import com.sdp.irrigation.db.irrigation_eq_real_time.service.IIrrigationEqRealTimeService;
import com.sdp.irrigation.db.irrigation_eq_valve.service.IIrrigationEqValveService;
import com.sdp.irrigation.entity.IrrigationEmp;
import com.sdp.irrigation.entity.IrrigationEqLogUserStatus;
import com.sdp.irrigation.entity.IrrigationEqOrder;
import com.sdp.irrigation.entity.IrrigationEqRealTime;
import com.sdp.irrigation.entity.IrrigationEqValve;
import com.sdp.irrigation.kafka.biz.IProtocolBiz;
import com.sdp.irrigation.kafka.producer.KafkaProducer;
import com.sdp.irrigation.kafka.property.ServerProperty;
import com.sdp.irrigation.kafka.utils.ProtocolUtils;
import com.sdp.irrigation.protocol.CommPackage;
import com.sdp.irrigation.utils.BeanUtils;
import com.sdp.irrigation.utils.CommConstants;
import com.sdp.irrigation.utils.IrrigationConstants;
import com.sdp.irrigation.utils.JsonUtils;
import com.sdp.irrigation.utils.ServerResponseEnum;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class UserBiz implements IProtocolBiz {

	@Autowired
	private IIrrigationEqLogUserStatusService eqLogUserStatusService;
	
	@Autowired
	private EqRealTimeCache eqRealTimeCache;
	
	@Autowired
	private EqValveCache eqValveCache;
	
	@Autowired
	private IIrrigationEqValveService eqValveService;
	
	@Autowired 
	private IIrrigationEqRealTimeService eqRealTimeService;
	
	@Autowired 
	private IIrrigationEqOrderService eqOrderService;
	
	@Autowired
	private KafkaProducer kafkaProducer;
	
	@Autowired
	private ServerProperty serverProperty;
	
	@Autowired
	private RedissonClient redissonclient;
	
	@Autowired 
	private IIrrigationEmpService empService;
	
	@Autowired 
	private EmpCache empCache;
	
	@Autowired
	private RedisUtil redisUtil;
	
//	private static final String NO_IC_NO_MAP_KEY = "_none_ic_no";

	
	private String getLogMapKey(String devSn, String pakSn) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(devSn).append("#").append(pakSn);
		return sbf.toString();
	}
	
	private void fillEqCacheField(IrrigationEqRealTime eqRealTime, IrrigationEqRealTime eqRealTimeUpdate, IrrigationEqLogUserStatus eqLogUserStatus, Map<String,IrrigationEqValve> eqValveMap, Map<String,IrrigationEqValve> eqValveTempMap) {
		//设备上报的时间不准，优先使用服务器的更新时间
		eqRealTimeUpdate.setUpdateTime(eqLogUserStatus.getUpdateTime());
//		eqRealTimeUpdate.setUpdateTime(eqLogUserStatus.getUploadTime());
		eqRealTimeUpdate.setOnlineStatus(IrrigationConstants.设备在线状态_在线.getCode());
		eqRealTimeUpdate.setEmpIc(eqLogUserStatus.getIcNo());
		eqRealTime.setUpdateTime(eqRealTimeUpdate.getUpdateTime());
		eqRealTime.setOnlineStatus(eqRealTimeUpdate.getOnlineStatus());
		eqRealTime.setEmpIc(eqLogUserStatus.getIcNo());
		
		if (!StringUtils.isEmpty(eqLogUserStatus.getDevStatus())) {
			String reversedStatus = new StringBuilder(eqLogUserStatus.getDevStatus()).reverse().toString();
			// 3-灌溉泵状态
			eqRealTimeUpdate.setIrrigationStatus(getStatus(reversedStatus, 3));
			eqRealTime.setIrrigationStatus(eqRealTimeUpdate.getIrrigationStatus());
			// 1-搅拌泵状态
			eqRealTimeUpdate.setStirringStatus(getStatus(reversedStatus, 1));
			eqRealTime.setStirringStatus(eqRealTimeUpdate.getStirringStatus());
			// 2-施肥泵状态
			eqRealTimeUpdate.setFertilizationStatus(getStatus(reversedStatus, 2));
			eqRealTime.setFertilizationStatus(eqRealTimeUpdate.getFertilizationStatus());
			// 4-加水状态
			eqRealTimeUpdate.setWateringStatus(getStatus(reversedStatus, 4));
			eqRealTime.setWateringStatus(eqRealTimeUpdate.getWateringStatus());
			// 5-排沙状态
		    eqRealTimeUpdate.setDesiltingStatus(getStatus(reversedStatus, 5));
		    eqRealTime.setDesiltingStatus(eqRealTimeUpdate.getDesiltingStatus());
		    // 0-是否工作中
		    String workStatus = getStatus(reversedStatus, 0);
		    
		    //特殊处理订单信息
		    if (!StringUtils.isEmpty(eqLogUserStatus.getUserOrder())) {
		    	eqRealTime.setCurMoney(eqLogUserStatus.getBatVol());//特殊处理
		    	eqRealTime.setCurWater(eqLogUserStatus.getCurWater());
		    	eqRealTime.setCurEle(eqLogUserStatus.getCurEle());
		    	eqRealTimeUpdate.setCurMoney(eqLogUserStatus.getBatVol());
		    	eqRealTimeUpdate.setCurWater(eqLogUserStatus.getCurWater());
		    	eqRealTimeUpdate.setCurEle(eqLogUserStatus.getCurEle());
		    	
//		    	eqRealTimeUpdate.setOrderNo(eqLogUserStatus.getUserOrder());
//			    eqRealTime.setOrderNo(eqLogUserStatus.getUserOrder());
			}
		    
		    eqRealTimeUpdate.setWorkStatus(workStatus);
		    eqRealTime.setWorkStatus(eqRealTimeUpdate.getWorkStatus());
		    
		    for (Entry<String,IrrigationEqValve> entry : eqValveTempMap.entrySet()) {
				IrrigationEqValve eqValve = entry.getValue();
				eqValve.setUpdateTime(LocalDateTime.now());
				eqValve.setSwitchStatus(getStatus(reversedStatus, 5 + eqValve.getSerialNo()));
				String valveMapKey = getEqValveMapKey(eqRealTime.getEqNo(), entry.getKey());
				if (!eqValveMap.containsKey(valveMapKey)) {
					eqValveMap.put(valveMapKey, eqValve);
				}
			}
		}
		
		
	}
	
	private void updateEqStatusCache(Map<String, IrrigationEqRealTime> eqRealTimeUpdateMap, Map<String, IrrigationEqValve> eqValveUpdateMap,
			IrrigationEqLogUserStatus eqLogUserStatus, IrrigationEqRealTime eqRealTime, Map<String, IrrigationEqValve> eqValveTempMap,
			IrrigationEqRealTime eqRealTimeUpdate) {
//		IrrigationEqRealTime eqRealTime = eqRealTimeCache.getEqRealTimeCache(eqLogUserStatus.getEqNo());
//		Map<String, IrrigationEqValve> eqValveTempMap = eqValveCache.getEqValveRealTimeCache(eqLogUserStatus.getEqNo());
		
		if (eqRealTime != null && !StringUtils.isEmpty(eqRealTime.getEqNo())) {
			//新建对象，对缓存进行部分更新
//			IrrigationEqRealTime eqRealTimeUpdate = new IrrigationEqRealTime();		
			fillEqCacheField(eqRealTime, eqRealTimeUpdate, eqLogUserStatus, eqValveUpdateMap, eqValveTempMap);
			eqRealTimeCache.updateEqRealTimeCache(eqRealTime.getEqNo(), BeanUtils.redisBean2Map(eqRealTimeUpdate));
			eqValveCache.updateEqValveCache(eqRealTime.getEqNo(), eqValveTempMap);
			
			//添加设备缓存的更新信息
			if (!eqRealTimeUpdateMap.containsKey(eqRealTime.getEqNo())) {
				eqRealTimeUpdateMap.put(eqRealTime.getEqNo(), eqRealTime);
			}
		}
	}
	
	private IrrigationEqOrder prepareEqOrder(IrrigationEqLogUserStatus eqLogUserStatus, IrrigationEqRealTime eqCache,
			boolean isEqCacheOrder) {
		IrrigationEqOrder eqOrder = new IrrigationEqOrder();
		eqOrder.setEqNo(eqLogUserStatus.getEqNo());
		eqOrder.setEndTime(LocalDateTime.now());		
		eqOrder.setCreateTime(LocalDateTime.now());
		eqOrder.setUpdateTime(LocalDateTime.now());
		if (isEqCacheOrder) {
			eqOrder.setIcNo(eqCache.getEmpIc());
			eqOrder.setOrderNo(eqCache.getOrderNo());
			eqOrder.setCurEle(eqCache.getCurEle());
			eqOrder.setCurMoney(eqCache.getCurMoney());
			eqOrder.setCurWater(eqCache.getCurWater());
			eqOrder.setStartTime(eqCache.getOrderStart());
		} else {
			eqOrder.setIcNo(eqLogUserStatus.getIcNo());
			eqOrder.setOrderNo(eqLogUserStatus.getUserOrder());
			eqOrder.setCurEle(eqLogUserStatus.getCurEle());
			eqOrder.setCurMoney(eqLogUserStatus.getBatVol());
			eqOrder.setCurWater(eqLogUserStatus.getCurWater());
			if (eqLogUserStatus.getUserOrder().equals(eqCache.getOrderNo())) {
				eqOrder.setStartTime(eqCache.getOrderStart());
			}
		}
		
		
		return eqOrder;
	}
	
	@Override
	public void execute(List<CommPackage> packageList) {
		if (packageList.size() > 0) {
			Map<String, List<IrrigationEqLogUserStatus>> icNoMap = new HashMap<>();//key是icNo
			Map<String, Map<String, IrrigationEqRealTime>> icNo2eqNoMap = new HashMap<>();//key是icNo,valueMap:key是设备编号
			Map<String, Map<String, Map<String, IrrigationEqValve>>> icNo2ValveNoMap = new HashMap<>();//key是icNo,valueMap:key是设备编号，value是所有阀门的map对象
			Map<String, IrrigationEqLogUserStatus> userStatusMap = new HashMap<>();//key是getLogMapKey
			Map<String, Map<String, Object>> userResponseMap = new HashMap<>();//key是getLogMapKey，每条消息都给应答
			Map<String, IrrigationEqRealTime> eqRealTimeUpdateMap = new HashMap<>();//设备缓存更新对象
			Map<String, IrrigationEqValve> eqValveUpdateMap = new HashMap<>();//设备阀门更新对象
			
			for (CommPackage commPackage : packageList) {
				String devSn = CommConstants.getUserCommPackageDevSn(commPackage);
				IrrigationEqLogUserStatus eqLogUserStatus = new IrrigationEqLogUserStatus();
				fillFieldValue(eqLogUserStatus, commPackage);
				String userMapKey = getLogMapKey(devSn, commPackage.getPakSn());
				userStatusMap.put(userMapKey, eqLogUserStatus);
				//根据ic卡号分类存储报文，卡号为空或者省平台的卡不管
				if (!StringUtils.isEmpty(eqLogUserStatus.getIcNo()) &&
						!CommConstants.isProvinceIcNo(eqLogUserStatus.getIcNo())) {
					if (!icNoMap.containsKey(eqLogUserStatus.getIcNo())) {
						List<IrrigationEqLogUserStatus> tempList = new ArrayList<>();
						icNoMap.put(eqLogUserStatus.getIcNo(), tempList);
					}
					icNoMap.get(eqLogUserStatus.getIcNo()).add(eqLogUserStatus);
					
					//收集设备缓存
					if (!icNo2eqNoMap.containsKey(eqLogUserStatus.getIcNo())) {
						Map<String, IrrigationEqRealTime> tempCacheMap = new HashMap<>();
						icNo2eqNoMap.put(eqLogUserStatus.getIcNo(), tempCacheMap);
					}
					if (!icNo2eqNoMap.get(eqLogUserStatus.getIcNo()).containsKey(eqLogUserStatus.getEqNo())) {
						IrrigationEqRealTime eqRealTime = eqRealTimeCache.getEqRealTimeCache(eqLogUserStatus.getEqNo());
						if (eqRealTime != null && !StringUtils.isEmpty(eqRealTime.getEqNo())) {
							icNo2eqNoMap.get(eqLogUserStatus.getIcNo()).put(eqLogUserStatus.getEqNo(), eqRealTime);
						}
					}
					//收集阀门缓存
					if (!icNo2ValveNoMap.containsKey(eqLogUserStatus.getIcNo())) {
						Map<String, Map<String, IrrigationEqValve>> tempValveMap = new HashMap<>();
						icNo2ValveNoMap.put(eqLogUserStatus.getIcNo(), tempValveMap);
					}
					if (!icNo2ValveNoMap.get(eqLogUserStatus.getIcNo()).containsKey(eqLogUserStatus.getEqNo())) {
						Map<String, IrrigationEqValve> eqValveTempMap = eqValveCache.getEqValveRealTimeCache(eqLogUserStatus.getEqNo());
						icNo2ValveNoMap.get(eqLogUserStatus.getIcNo()).put(eqLogUserStatus.getEqNo(), eqValveTempMap);
					}
					
					
				} else {
					//没有订单号或者省平台的卡号，不需要处理
					Map<String, Object> responseMap = getResponseMap(devSn, ServerResponseEnum.USER_RESPONSE.getCode(), 0);
					userResponseMap.put(userMapKey, responseMap);
					//更新下缓存状态
					IrrigationEqRealTime eqRealTime = eqRealTimeCache.getEqRealTimeCache(eqLogUserStatus.getEqNo());
					Map<String, IrrigationEqValve> eqValveTempMap = eqValveCache.getEqValveRealTimeCache(eqLogUserStatus.getEqNo());
					IrrigationEqRealTime eqRealTimeUpdate = new IrrigationEqRealTime();
					updateEqStatusCache(eqRealTimeUpdateMap, eqValveUpdateMap, eqLogUserStatus, eqRealTime, eqValveTempMap, eqRealTimeUpdate);
				}
			}
			
			//没有订单号的报文不需要判断可以直接先响应
			if (userResponseMap.size() > 0) {
				for (Map<String, Object> data : userResponseMap.values()) {
					sendKafka(data);
				}
				userResponseMap.clear();
			}
			
			if (eqRealTimeUpdateMap.size() > 0) {
				eqRealTimeService.updateBatchById(new ArrayList<>(eqRealTimeUpdateMap.values()));
				eqRealTimeUpdateMap.clear();
			}
			
			if (eqValveUpdateMap.size() > 0) {
				eqValveService.updateBatchById(new ArrayList<>(eqValveUpdateMap.values()));
				eqValveUpdateMap.clear();
			}
			
			if (userStatusMap.size() > 0) {
				eqLogUserStatusService.saveBatch(new ArrayList<>(userStatusMap.values()));
			}
			
			//开始业务处理
			for (Entry<String, List<IrrigationEqLogUserStatus>> entry : icNoMap.entrySet()) {
				String lockKey = IrrigationCacheKeys.getIcNoLockCacheKey(entry.getKey());
				RLock lock = redissonclient.getLock(lockKey);
				try {
					if(lock.tryLock(10, TimeUnit.SECONDS)) {
						processOrdersBatch(entry.getKey(), entry.getValue(), eqRealTimeUpdateMap, eqValveUpdateMap, icNo2eqNoMap.get(entry.getKey()), icNo2ValveNoMap.get(entry.getKey()));
					}
				}
				catch (Exception e) {
					log.error("---------user帧锁处理业务报错");
					CommConstants.printErrorLog(e, log);
				} 
				finally {
					lock.unlock();
				}
				
			}
		}
	}
	
	private boolean isOrderCacheSettled(String userOrder) {
		return redisUtil.hasKey(IrrigationCacheKeys.getSettledOrderCacheKey(userOrder));
	}
	
	private void setOrderCacheSettled(String userOrder) {
		redisUtil.set(IrrigationCacheKeys.getSettledOrderCacheKey(userOrder), "1", 3600);
	}
	
	public boolean isOrderDBSettled(String userOrder) {
		QueryWrapper<IrrigationEqOrder> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(IrrigationEqOrder::getOrderNo, userOrder);
		IrrigationEqOrder eqOrder = eqOrderService.getOne(queryWrapper, false);
		if (eqOrder != null) {
			return true;
		}
		return false;
	}
	
	private boolean isRechargeEq(String eqNo) {
		return eqNo.toLowerCase().startsWith("f");
	}
	
	@Transactional
	public void processOrdersBatch(String icNo, List<IrrigationEqLogUserStatus> logList,
			Map<String, IrrigationEqRealTime> eqRealTimeUpdateMap, Map<String, IrrigationEqValve> eqValveUpdateMap,
			Map<String, IrrigationEqRealTime> eqCacheMap, Map<String, Map<String, IrrigationEqValve>> eqValveCacheMap) {
		//先获取ic卡信息
		IrrigationEmp emp = empCache.getEmp(icNo);
		//用户ic卡数据库表更新map
		Map<String, IrrigationEmp> updateEmpMap = new HashMap<>();
		//结账订单
		List<IrrigationEqOrder> orderInsertList = new ArrayList<>();
		if (emp != null && !StringUtils.isEmpty(emp.getEmpIc())) {
			for (IrrigationEqLogUserStatus userStatusLog : logList) {
				//获取设备缓存
				IrrigationEqRealTime eqCache = eqCacheMap.get(userStatusLog.getEqNo());
				if (eqCache != null) {//设备缓存存在再进行业务处理，要不然可以什么都不做
					String settleStatus = userStatusLog.getSettleStatus();
//					String reversedStatus = new StringBuilder(userStatusLog.getDevStatus()).reverse().toString();
//					String workStatus = getStatus(reversedStatus, 0);
					
					if (IrrigationConstants.是.getCode().equals(settleStatus)) {//结账单据
						//先判断去重
						if ((userStatusLog.getUserOrder().equals(eqCache.getOrderNo()) && IrrigationConstants.是.getCode().equals(eqCache.getOrderStatus()))
								|| isOrderCacheSettled(userStatusLog.getUserOrder())
								|| isOrderDBSettled(userStatusLog.getUserOrder())) {
							log.info("#############Settled order {} already processed, skipping", eqCache.getOrderNo());
							//直接响应应答报文
							Map<String, Object> responseMap = getResponseMap(userStatusLog.getEqNo(), ServerResponseEnum.USER_SETTLE_RESPONSE.getCode(), 0);
							sendKafka(responseMap);
							continue;
						}
						//判断设备类型
						if (isRechargeEq(userStatusLog.getEqNo())) {//如果是充值设备
							//更新用户ic卡余额
							if (emp.getIcMoney() == null) {
								emp.setIcMoney(userStatusLog.getBatVol());
							} else {
								emp.setIcMoney(emp.getIcMoney().add(userStatusLog.getBatVol()));
							}
							
						} else {//灌溉设备
							//更新用户ic卡余额
							BigDecimal currentBalance = emp.getIcMoney() == null ? BigDecimal.ZERO : emp.getIcMoney();
							if (currentBalance.compareTo(userStatusLog.getBatVol()) < 0) {
								log.error("############Insufficient balance for order {}, cardNo: {}, curMoney: {}, icMoney: {}", userStatusLog.getUserOrder(), userStatusLog.getIcNo(), userStatusLog.getBatVol(), emp.getIcMoney());
								throw new RuntimeException("Insufficient balance");
							} else {
								emp.setIcMoney(currentBalance.subtract(userStatusLog.getBatVol()));
							}
						}
						
						//新增订单
						IrrigationEqOrder eqOrder = prepareEqOrder(userStatusLog, eqCache, false);
						orderInsertList.add(eqOrder);
						//更新用户余额
						IrrigationEmp updateCacheEmp = new IrrigationEmp();
						updateCacheEmp.setIcMoney(emp.getIcMoney());
						empCache.updateEmpCache(icNo, BeanUtils.redisBean2Map(updateCacheEmp));
						updateEmpMap.put(emp.getEmpIc(), emp);
						
						//添加结账缓存
						setOrderCacheSettled(userStatusLog.getUserOrder());
						
						//更新设备缓存
						Map<String, IrrigationEqValve> eqValveTempMap = eqValveCache.getEqValveRealTimeCache(userStatusLog.getEqNo());
						IrrigationEqRealTime eqRealTimeUpdate = new IrrigationEqRealTime();
						//先更新设备订单信息
						eqCache.setOrderEnd(userStatusLog.getUpdateTime());
						eqCache.setOrderNo(userStatusLog.getUserOrder());
						eqCache.setOrderStatus(IrrigationConstants.是.getCode());
						eqRealTimeUpdate.setOrderEnd(userStatusLog.getUpdateTime());
						eqRealTimeUpdate.setOrderNo(userStatusLog.getUserOrder());
						eqRealTimeUpdate.setOrderStatus(IrrigationConstants.是.getCode());
						updateEqStatusCache(eqRealTimeUpdateMap, eqValveUpdateMap, userStatusLog, eqCache, eqValveTempMap, eqRealTimeUpdate);

						//响应应答报文
						Map<String, Object> responseMap = getResponseMap(userStatusLog.getEqNo(), ServerResponseEnum.USER_SETTLE_RESPONSE.getCode(), 0);
						sendKafka(responseMap);
						
					} else {//实时订单信息
						Integer tempMoney = 0;
						if (!isRechargeEq(userStatusLog.getEqNo())) {//只处理非充值机设备
							//特殊处理设备实时状态中老的未结账的单子
							if (!StringUtils.isEmpty(eqCache.getOrderNo()) 
									&& !userStatusLog.getUserOrder().equals(eqCache.getOrderNo())
									&& IrrigationConstants.是.getCode().equals(eqCache.getWorkStatus())) {
								//更新用户ic卡余额
								BigDecimal currentBalance = emp.getIcMoney() == null ? BigDecimal.ZERO : emp.getIcMoney();
								if (currentBalance.compareTo(userStatusLog.getBatVol()) < 0) {
									log.error("###############Insufficient balance for order {}, cardNo: {}, curMoney: {}, userIcMoney: {}", userStatusLog.getUserOrder(), userStatusLog.getIcNo(), userStatusLog.getBatVol(), emp.getIcMoney());
									throw new RuntimeException("Insufficient balance");
								} else {
									emp.setIcMoney(currentBalance.subtract(userStatusLog.getBatVol()));
								}
								//新增订单
								IrrigationEqOrder eqOrder = prepareEqOrder(userStatusLog, eqCache, true);
								orderInsertList.add(eqOrder);
								//更新用户余额
								IrrigationEmp updateCacheEmp = new IrrigationEmp();
								updateCacheEmp.setIcMoney(emp.getIcMoney());
								empCache.updateEmpCache(icNo, BeanUtils.redisBean2Map(updateCacheEmp));
								updateEmpMap.put(emp.getEmpIc(), emp);

								//添加结账缓存
								setOrderCacheSettled(userStatusLog.getUserOrder());
								
							}
							
							BigDecimal deductionBalance = BigDecimal.ZERO;
							//获取最新的实时预扣余额
							if (userStatusLog.getBatVol() != null) {
								deductionBalance = deductionBalance.add(userStatusLog.getBatVol());
							}
							List<IrrigationEqRealTime> eqRealTimeList = queryWorkingEqCacheByIcNo(icNo);
							for (IrrigationEqRealTime irrigationEqRealTime : eqRealTimeList) {
								if (!irrigationEqRealTime.getEqNo().equals(userStatusLog.getEqNo())) {
									if (irrigationEqRealTime.getCurMoney() != null) {
										deductionBalance = deductionBalance.add(irrigationEqRealTime.getCurMoney());
									}
								}
//								if (irrigationEqRealTime.getEqNo().equals(userStatusLog.getEqNo())) {
//									deductionBalance = deductionBalance.add(userStatusLog.getBatVol());
//								} else {
//									if (irrigationEqRealTime.getCurMoney() != null) {
//										deductionBalance = deductionBalance.add(irrigationEqRealTime.getCurMoney());
//									}
//								}
							}
							
							BigDecimal currentBalance = emp.getIcMoney() == null ? BigDecimal.ZERO : emp.getIcMoney();
							if (deductionBalance.compareTo(currentBalance) > 0) {
								log.error("##############Insufficient deduction balance for deductionBalance {}, userIcMoney: {}", deductionBalance, currentBalance);
							} else {
								tempMoney = currentBalance.subtract(deductionBalance).intValue();
							}

						}

						//更新设备缓存
						Map<String, IrrigationEqValve> eqValveTempMap = eqValveCache.getEqValveRealTimeCache(userStatusLog.getEqNo());
						IrrigationEqRealTime eqRealTimeUpdate = new IrrigationEqRealTime();
						//先更新设备订单信息
						if (StringUtils.isEmpty(eqCache.getOrderNo()) || !eqCache.getOrderNo().endsWith(userStatusLog.getUserOrder())) {
							eqCache.setOrderStart(userStatusLog.getUpdateTime());
							eqCache.setOrderNo(userStatusLog.getUserOrder());
							eqCache.setOrderStatus(IrrigationConstants.否.getCode());
							eqRealTimeUpdate.setOrderStart(userStatusLog.getUpdateTime());
							eqRealTimeUpdate.setOrderNo(userStatusLog.getUserOrder());
							eqRealTimeUpdate.setOrderStatus(IrrigationConstants.否.getCode());
						}
						updateEqStatusCache(eqRealTimeUpdateMap, eqValveUpdateMap, userStatusLog, eqCache, eqValveTempMap, eqRealTimeUpdate);
						
						//响应应答报文
						Map<String, Object> responseMap = getResponseMap(userStatusLog.getEqNo(), ServerResponseEnum.USER_RESPONSE.getCode(), tempMoney);
						sendKafka(responseMap);
					}
				}
				
			}
			
		} else {//如果用户ic卡号对应的实体对象不存在，不处理直接应答
			for (IrrigationEqLogUserStatus userStatusLog : logList) {
				Map<String, Object> responseMap = getResponseMap(userStatusLog.getEqNo(), ServerResponseEnum.USER_RESPONSE.getCode(), 0);
				sendKafka(responseMap);
				//更新下缓存状态
				IrrigationEqRealTime eqRealTime = eqRealTimeCache.getEqRealTimeCache(userStatusLog.getEqNo());
				Map<String, IrrigationEqValve> eqValveTempMap = eqValveCache.getEqValveRealTimeCache(userStatusLog.getEqNo());
				IrrigationEqRealTime eqRealTimeUpdate = new IrrigationEqRealTime();
				updateEqStatusCache(eqRealTimeUpdateMap, eqValveUpdateMap, userStatusLog, eqRealTime, eqValveTempMap, eqRealTimeUpdate);
			}
		}
		
		//更新缓存的数据库表
		if (eqRealTimeUpdateMap.size() > 0) {
			eqRealTimeService.updateBatchById(new ArrayList<>(eqRealTimeUpdateMap.values()));
			eqRealTimeUpdateMap.clear();
		}
		
		if (eqValveUpdateMap.size() > 0) {
			eqValveService.updateBatchById(new ArrayList<>(eqValveUpdateMap.values()));
			eqValveUpdateMap.clear();
		}
		
		if (updateEmpMap.size() > 0) {
			empService.updateBatchById(new ArrayList<>(updateEmpMap.values()));
			updateEmpMap.clear();
		}
		
		if (orderInsertList.size() > 0) {
			eqOrderService.saveBatch(orderInsertList);
			orderInsertList.clear();
		}
	}
	
	private List<IrrigationEqRealTime> queryWorkingEqCacheByIcNo(String icNo) {
		QueryWrapper<IrrigationEqRealTime> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(IrrigationEqRealTime::getEmpIc, icNo)
			.eq(IrrigationEqRealTime::getOnlineStatus, IrrigationConstants.是.getCode())
			.and(qw -> qw.ne(IrrigationEqRealTime::getOrderStatus, IrrigationConstants.是.getCode())
	                   .or()
	                   .isNull(IrrigationEqRealTime::getOrderStatus))
			.eq(IrrigationEqRealTime::getWorkStatus, IrrigationConstants.是.getCode());
		return eqRealTimeService.list(queryWrapper);
	}
	
	private void sendKafka(Map<String, Object> data) {
		//暂时都用用户帧应答的topic
		kafkaProducer.send(ProtocolUtils.getDownMessageTopic(String.valueOf(ServerResponseEnum.USER_RESPONSE.getCode()), serverProperty.getDownTopicPrefix()), JsonUtils.toJSONString(data));
//		if (ServerResponseEnum.BALANCE_RESULT.getCode() == code) {
//			kafkaProducer.send(ProtocolUtils.getDownMessageTopic(String.valueOf(ServerResponseEnum.BALANCE_RESULT.getCode()), serverProperty.getDownTopicPrefix()), JsonUtils.toJSONString(data));
//		} else {
//			kafkaProducer.send(ProtocolUtils.getDownMessageTopic(String.valueOf(ServerResponseEnum.BALANCE_RESULT.getCode()), serverProperty.getDownTopicPrefix()), JsonUtils.toJSONString(data));
//		}
		
	}
	
	private Map<String, Object> getResponseMap(String devSn, int code, Integer money) {
		//ServerResponseEnum.USER_RESPONSE.getCode()
		Map<String, Object> map = new LinkedHashMap<>();
		map.put("pakSn", CommConstants.generatePakSn());
		map.put("code", code);
		map.put("devSn", devSn);
		if (money != null) {
			map.put("money", money);
		}
		return map;
	} 

	private void fillFieldValue(IrrigationEqLogUserStatus eqLogUserStatus, CommPackage commPackage) {
		eqLogUserStatus.setCreateTime(LocalDateTime.now());
		eqLogUserStatus.setUpdateTime(LocalDateTime.now());
		eqLogUserStatus.setProtocolContent(JsonUtils.toJSONString(commPackage));
		//设备端时间不准
		eqLogUserStatus.setUploadTime(eqLogUserStatus.getCreateTime());
		
		JSONObject body = commPackage.getBody();
		if (body !=null) {
			JSONObject devObj = body.getJSONObject("dev");
			if (devObj != null) {
				if (devObj.containsKey("devSn")) {
					eqLogUserStatus.setEqNo(devObj.getString("devSn"));
				}
				if (devObj.containsKey("devStatus")) {
					eqLogUserStatus.setDevStatus(devObj.getString("devStatus"));
				}
				if (devObj.containsKey("sumWater")) {
					eqLogUserStatus.setSumWater(new BigDecimal(devObj.getString("sumWater")));
				}
				if (devObj.containsKey("sumEle")) {
					eqLogUserStatus.setSumEle(new BigDecimal(devObj.getString("sumEle")));
				}
				if (devObj.containsKey("batVol")) {//由电压变为最新消费总金额，单位是分
					eqLogUserStatus.setBatVol(new BigDecimal(devObj.getIntValue("batVol")));
				} else {
					eqLogUserStatus.setBatVol(BigDecimal.ZERO);
				}
				if (devObj.containsKey("gprsRssi")) {
					eqLogUserStatus.setGprsRssi(devObj.getString("gprsRssi"));
				}
				if (devObj.containsKey("Ua")) {
					eqLogUserStatus.setUa(new BigDecimal(devObj.getString("Ua")));
				}
				if (devObj.containsKey("Ub")) {
					eqLogUserStatus.setUb(new BigDecimal(devObj.getString("Ub")));
				}
				if (devObj.containsKey("Uc")) {
					eqLogUserStatus.setUc(new BigDecimal(devObj.getString("Uc")));
				}
				if (devObj.containsKey("Ia")) {
					eqLogUserStatus.setIa(new BigDecimal(devObj.getString("Ia")));
				}
				if (devObj.containsKey("Ib")) {
					eqLogUserStatus.setIb(new BigDecimal(devObj.getString("Ib")));
				}
				if (devObj.containsKey("Ic")) {
					eqLogUserStatus.setIc(new BigDecimal(devObj.getString("Ic")));
				}
				if (devObj.containsKey("Pt")) {
					eqLogUserStatus.setPt(new BigDecimal(devObj.getString("Pt")));
				}
			}
			
			JSONObject cardObj = body.getJSONObject("card");
			if (cardObj != null) {
				if (cardObj.containsKey("order")) {
					eqLogUserStatus.setUserOrder(cardObj.getString("order"));
				}
				if (cardObj.containsKey("action")) {
					eqLogUserStatus.setUserAction(String.valueOf(cardObj.getInteger("action")));
				}
				if (cardObj.containsKey("cardNum")) {
					//这个是ic卡号么
					eqLogUserStatus.setIcNo(cardObj.getString("cardNum"));
				}
				if (cardObj.containsKey("area")) {
					eqLogUserStatus.setIcArea(String.valueOf(cardObj.getInteger("area")));
				}
				if (cardObj.containsKey("curMoney")) {
					eqLogUserStatus.setCurMoney(new BigDecimal(cardObj.getInteger("curMoney")));
				}
				if (cardObj.containsKey("curWater")) {
					eqLogUserStatus.setCurWater(new BigDecimal(cardObj.getInteger("curWater")));
				}
				if (cardObj.containsKey("curEle")) {
					eqLogUserStatus.setCurEle(new BigDecimal(cardObj.getInteger("curEle")));
				}
				if (cardObj.containsKey("sumWater")) {
					//跟设备状态中的值，有什么区别
					eqLogUserStatus.setSumWater(new BigDecimal(cardObj.getInteger("sumWater")));
				}
				if (cardObj.containsKey("sumEle")) {
					//跟设备状态中的值，有什么区别
					eqLogUserStatus.setSumEle(new BigDecimal(cardObj.getInteger("sumEle")));
				}
				if (cardObj.containsKey("time")) {
					eqLogUserStatus.setSettleStatus(String.valueOf(cardObj.getInteger("time")));
				}
			}
		}
	}
	
//	@Override
//	public void execute(List<CommPackage> packageList) {
//		if (packageList.size() > 0) {
//			List<IrrigationEqLogUserStatus> insertList = new ArrayList<>();
//			Map<String, IrrigationEqRealTime> eqRealTimeMap = new HashMap<>();
//			Map<String, IrrigationEqValve> eqValveMap = new HashMap<>();
//			
//			for (CommPackage commPackage : packageList) {
//				String devSn = CommConstants.getUserCommPackageDevSn(commPackage);
//				IrrigationEqLogUserStatus eqLogUserStatus = new IrrigationEqLogUserStatus();
//				eqLogUserStatus.setEqNo(devSn);
//				copyFieldValue(eqLogUserStatus, commPackage);
//				eqLogUserStatus.setCreateTime(LocalDateTime.now());
//				eqLogUserStatus.setUpdateTime(LocalDateTime.now());
//				eqLogUserStatus.setProtocolContent(JsonUtils.toJSONString(commPackage));
//				//上报时间的处理
//				if (!StringUtils.isEmpty(commPackage.getPakSn())) {
//					eqLogUserStatus.setUploadTime(CommConstants.pakSn2LocalDateTime(commPackage.getPakSn()));
//				}
//				insertList.add(eqLogUserStatus);
//				
//				//处理缓存
//				IrrigationEqRealTime eqRealTime = eqRealTimeCache.getEqRealTimeCache(devSn);
//				Map<String, IrrigationEqValve> eqValveTempMap = eqValveCache.getEqValveRealTimeCache(devSn);
//				
//				if (eqRealTime != null && !StringUtils.isEmpty(eqRealTime.getEqNo())) {
//					//更新缓存的信息
//					
//					//新建对象，对缓存进行部分更新
//					IrrigationEqRealTime eqRealTimeUpdate = new IrrigationEqRealTime();
//					updateEqStatus(eqRealTime, eqRealTimeUpdate, eqLogUserStatus, eqValveMap, eqValveTempMap);
//					eqRealTimeCache.updateEqRealTimeCache(devSn, BeanUtils.redisBean2Map(eqRealTimeUpdate));
//					eqValveCache.updateEqValveCache(devSn, eqValveTempMap);
//					
//					//更新数据库信息
//					if (!eqRealTimeMap.containsKey(devSn)) {
//						eqRealTimeMap.put(devSn, eqRealTime);
//					}
//				}
//							
//			}
//			
//			if (eqRealTimeMap.size() > 0) {
//				eqRealTimeService.updateBatchById(new ArrayList<>(eqRealTimeMap.values()));
//			}
//			
//			if (eqValveMap.size() > 0) {
//				eqValveService.updateBatchById(new ArrayList<>(eqValveMap.values()));
//			}
//			
//			if (insertList.size() > 0) {
//				eqLogUserStatusService.saveBatch(insertList);
//			}
//			
//		}
//		
//	}
	
//	private void updateEqStatus(IrrigationEqRealTime eqRealTime, IrrigationEqRealTime eqRealTimeUpdate, IrrigationEqLogUserStatus eqLogUserStatus, Map<String,IrrigationEqValve> eqValveMap, Map<String,IrrigationEqValve> eqValveTempMap) {
//		//设备上报的时间不准，优先使用服务器的更新时间
//		eqRealTimeUpdate.setUpdateTime(eqLogUserStatus.getUpdateTime());
////		eqRealTimeUpdate.setUpdateTime(eqLogUserStatus.getUploadTime());
//		eqRealTimeUpdate.setOnlineStatus(IrrigationConstants.设备在线状态_在线.getCode());
//		eqRealTimeUpdate.setEmpIc(eqLogUserStatus.getIcNo());
//		eqRealTime.setUpdateTime(eqRealTimeUpdate.getUpdateTime());
//		eqRealTime.setOnlineStatus(eqRealTimeUpdate.getOnlineStatus());
//		eqRealTime.setEmpIc(eqLogUserStatus.getIcNo());
//
//		if (!StringUtils.isEmpty(eqLogUserStatus.getDevStatus())) {
//			String reversedStatus = new StringBuilder(eqLogUserStatus.getDevStatus()).reverse().toString();
//			// 3-灌溉泵状态
//			eqRealTimeUpdate.setIrrigationStatus(getStatus(reversedStatus, 3));
//			eqRealTime.setIrrigationStatus(eqRealTimeUpdate.getIrrigationStatus());
//			// 1-搅拌泵状态
//			eqRealTimeUpdate.setStirringStatus(getStatus(reversedStatus, 1));
//			eqRealTime.setStirringStatus(eqRealTimeUpdate.getStirringStatus());
//			// 2-施肥泵状态
//			eqRealTimeUpdate.setFertilizationStatus(getStatus(reversedStatus, 2));
//			eqRealTime.setFertilizationStatus(eqRealTimeUpdate.getFertilizationStatus());
//			// 4-加水状态
//			eqRealTimeUpdate.setWateringStatus(getStatus(reversedStatus, 4));
//			eqRealTime.setWateringStatus(eqRealTimeUpdate.getWateringStatus());
//			// 5-排沙状态
//		    eqRealTimeUpdate.setDesiltingStatus(getStatus(reversedStatus, 5));
//		    eqRealTime.setDesiltingStatus(eqRealTimeUpdate.getDesiltingStatus());
//		    // 0-是否工作中
//		    String workStatus = getStatus(reversedStatus, 0);
//		    //特殊处理订单信息
//		    if (!StringUtils.isEmpty(eqLogUserStatus.getUserOrder())) {
//		    	//设置订单中的钱，水，电
//		    	eqRealTime.setCurMoney(eqLogUserStatus.getCurMoney());
//		    	eqRealTime.setCurWater(eqLogUserStatus.getCurWater());
//		    	eqRealTime.setCurEle(eqLogUserStatus.getCurEle());
//		    	eqRealTimeUpdate.setCurMoney(eqLogUserStatus.getCurMoney());
//		    	eqRealTimeUpdate.setCurWater(eqLogUserStatus.getCurWater());
//		    	eqRealTimeUpdate.setCurEle(eqLogUserStatus.getCurEle());
//		    	
//		    	if (IrrigationConstants.是.getCode().equals(eqLogUserStatus.getSettleStatus())) {
//		    		IrrigationEqOrder eqOrder = new IrrigationEqOrder();
//		    		eqOrder.setEqNo(eqLogUserStatus.getEqNo());
//		    		eqOrder.setOrderNo(eqLogUserStatus.getUserOrder());
//		    		eqOrder.setStartTime(LocalDateTime.now());
//		    		eqOrder.setEndTime(LocalDateTime.now());
//		    		eqOrder.setCurEle(eqLogUserStatus.getCurEle());
//		    		eqOrder.setCurMoney(eqLogUserStatus.getBatVol());
//		    		eqOrder.setCurWater(eqLogUserStatus.getCurWater());
//		    		eqOrder.setCreateTime(LocalDateTime.now());
//		    		eqOrder.setUpdateTime(LocalDateTime.now());
//		    		eqOrderService.save(eqOrder);
//		    		//更新人员的ic卡余额
//		    		QueryWrapper<IrrigationEmp> queryWrapper = new QueryWrapper<>();
//		    		queryWrapper.lambda().eq(IrrigationEmp::getEmpIc, eqLogUserStatus.getIcNo());
//		    		IrrigationEmp emp = empService.getOne(queryWrapper, false);
//		    		if (emp != null) {
//						if (emp.getIcMoney() != null) {
//							emp.setIcMoney(eqOrder.getCurMoney() == null ? emp.getIcMoney() : emp.getIcMoney().add(eqOrder.getCurMoney()));
//						} else {
//							emp.setIcMoney(eqOrder.getCurMoney() == null ? BigDecimal.ZERO : BigDecimal.ZERO.add(eqOrder.getCurMoney()));
//						}
//						empService.updateById(emp);
//					}
//
////		    		UpdateWrapper<IrrigationEmp> updateWrapper = new UpdateWrapper<>();
////		    		updateWrapper.lambda().eq(IrrigationEmp::getEmpIc, eqLogUserStatus.getIcNo())
////		    			.set(IrrigationEmp::getIcMoney, eqLogUserStatus.getCurMoney());
////		    		empService.update(updateWrapper);
//				}
//		    	
////		    	if (IrrigationConstants.是.getCode().equals(workStatus) &&
////			    		(StringUtils.isEmpty(eqRealTime.getOrderNo()) || !eqRealTime.getOrderNo().equals(eqLogUserStatus.getUserOrder()))) {
////		    		//新增订单
////		    		IrrigationEqOrder eqOrder = new IrrigationEqOrder();
////		    		eqOrder.setEqNo(eqLogUserStatus.getEqNo());
////		    		eqOrder.setOrderNo(eqLogUserStatus.getUserOrder());
////		    		eqOrder.setStartTime(LocalDateTime.now());
////		    		eqOrder.setCurEle(eqLogUserStatus.getCurEle());
////		    		eqOrder.setCurMoney(eqLogUserStatus.getCurMoney());
////		    		eqOrder.setCurWater(eqLogUserStatus.getCurWater());
////		    		eqOrder.setCreateTime(LocalDateTime.now());
////		    		eqOrder.setUpdateTime(LocalDateTime.now());
////		    		eqOrderService.save(eqOrder);
////				} else if (IrrigationConstants.是.getCode().equals(workStatus) &&
////						IrrigationConstants.否.getCode().equals(workStatus)) {
////					QueryWrapper<IrrigationEqOrder> queryWrapper = new QueryWrapper<>();
////					queryWrapper.lambda().eq(IrrigationEqOrder::getEqNo, eqLogUserStatus.getEqNo())
////						.eq(IrrigationEqOrder::getOrderNo, eqLogUserStatus.getUserOrder());
////					IrrigationEqOrder irrigationEqOrder = eqOrderService.getOne(queryWrapper, false);
////					if (irrigationEqOrder != null) {
////						irrigationEqOrder.setEndTime(LocalDateTime.now());
////						irrigationEqOrder.setUpdateTime(LocalDateTime.now());
////						Duration duration = Duration.between(irrigationEqOrder.getStartTime(), irrigationEqOrder.getEndTime());
////				        long minutes = duration.toMinutes();
////				        irrigationEqOrder.setWorkTime((int)minutes);
////				        irrigationEqOrder.setCurEle(eqLogUserStatus.getCurEle());
////				        irrigationEqOrder.setCurMoney(eqLogUserStatus.getCurMoney());
////				        irrigationEqOrder.setCurWater(eqLogUserStatus.getCurWater());
////			    		eqOrderService.updateById(irrigationEqOrder);
////					}
////				}
//		    	
//		    	eqRealTimeUpdate.setOrderNo(eqLogUserStatus.getUserOrder());
//			    eqRealTime.setOrderNo(eqLogUserStatus.getUserOrder());
//			}
//		    
//		    eqRealTimeUpdate.setWorkStatus(workStatus);
//		    eqRealTime.setWorkStatus(eqRealTimeUpdate.getWorkStatus());
//
//			for (Entry<String,IrrigationEqValve> entry : eqValveTempMap.entrySet()) {
//				IrrigationEqValve eqValve = entry.getValue();
//				eqValve.setUpdateTime(LocalDateTime.now());
//				eqValve.setSwitchStatus(getStatus(reversedStatus, 5 + eqValve.getSerialNo()));
//				String valveMapKey = getEqValveMapKey(eqRealTime.getEqNo(), entry.getKey());
//				if (!eqValveMap.containsKey(valveMapKey)) {
//					eqValveMap.put(valveMapKey, eqValve);
//				}
//			}
//			
//		}
//	}
	
	public String getEqValveMapKey(String eqNo, String valveNo) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(eqNo).append("#").append(valveNo);
		return sbf.toString();
	}
	
	public static void main(String[] args) {
//		String status = "10000010000000000000000000000000";
//		System.out.println(status.charAt(6));
		LocalDateTime dateTime = LocalDateTime.now();
		System.out.println(dateTime);
		long time = LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond();
		System.out.println(time);
		time = new Date().getTime() / 1000;
		System.out.println(time);
		System.out.println(CommConstants.pakSn2LocalDateTime(String.valueOf(time)));
	}
	
	private static String getStatus(String status, int position) {
		//IrrigationConstants
		if (position < status.length()) {
			return Character.toString(status.charAt(position)); 
		}
		else {
			log.error("索引值： {}的值超出devStatus字符串长度： {}", position, status.length());
//			throw new RuntimeException("索引position的值超出devStatus字符串长度");
		}
		return IrrigationConstants.否.getCode();
	}
	
//	private void copyFieldValue(IrrigationEqLogUserStatus eqLogUserStatus, CommPackage commPackage) {
//		JSONObject body = commPackage.getBody();
//		if (body !=null) {
//			JSONObject devObj = body.getJSONObject("dev");
//			if (devObj != null) {
//				if (devObj.containsKey("devStatus")) {
//					eqLogUserStatus.setDevStatus(devObj.getString("devStatus"));
//				}
//				if (devObj.containsKey("sumWater")) {
//					eqLogUserStatus.setSumWater(new BigDecimal(devObj.getString("sumWater")));
//				}
//				if (devObj.containsKey("sumEle")) {
//					eqLogUserStatus.setSumEle(new BigDecimal(devObj.getString("sumEle")));
//				}
//				if (devObj.containsKey("batVol")) {//由电压变为最新消费总金额，单位是分
//					eqLogUserStatus.setBatVol(new BigDecimal(devObj.getIntValue("batVol")));
//				}
//				if (devObj.containsKey("gprsRssi")) {
//					eqLogUserStatus.setGprsRssi(devObj.getString("gprsRssi"));
//				}
//				if (devObj.containsKey("Ua")) {
//					eqLogUserStatus.setUa(new BigDecimal(devObj.getString("Ua")));
//				}
//				if (devObj.containsKey("Ub")) {
//					eqLogUserStatus.setUb(new BigDecimal(devObj.getString("Ub")));
//				}
//				if (devObj.containsKey("Uc")) {
//					eqLogUserStatus.setUc(new BigDecimal(devObj.getString("Uc")));
//				}
//				if (devObj.containsKey("Ia")) {
//					eqLogUserStatus.setIa(new BigDecimal(devObj.getString("Ia")));
//				}
//				if (devObj.containsKey("Ib")) {
//					eqLogUserStatus.setIb(new BigDecimal(devObj.getString("Ib")));
//				}
//				if (devObj.containsKey("Ic")) {
//					eqLogUserStatus.setIc(new BigDecimal(devObj.getString("Ic")));
//				}
//				if (devObj.containsKey("Pt")) {
//					eqLogUserStatus.setPt(new BigDecimal(devObj.getString("Pt")));
//				}
//			}
//			
//			JSONObject cardObj = body.getJSONObject("card");
//			if (cardObj != null) {
//				if (cardObj.containsKey("order")) {
//					eqLogUserStatus.setUserOrder(cardObj.getString("order"));
//				}
//				if (cardObj.containsKey("action")) {
//					eqLogUserStatus.setUserOrder(String.valueOf(cardObj.getInteger("action")));
//				}
//				if (cardObj.containsKey("cardNum")) {
//					//这个是ic卡号么
//					eqLogUserStatus.setIcNo(cardObj.getString("cardNum"));
//				}
//				if (cardObj.containsKey("area")) {
//					eqLogUserStatus.setIcArea(String.valueOf(cardObj.getInteger("area")));
//				}
//				if (cardObj.containsKey("curMoney")) {
//					eqLogUserStatus.setCurMoney(new BigDecimal(cardObj.getInteger("curMoney")));
//				}
//				if (cardObj.containsKey("curWater")) {
//					eqLogUserStatus.setCurWater(new BigDecimal(cardObj.getInteger("curWater")));
//				}
//				if (cardObj.containsKey("curEle")) {
//					eqLogUserStatus.setCurEle(new BigDecimal(cardObj.getInteger("curEle")));
//				}
//				if (cardObj.containsKey("sumWater")) {
//					//跟设备状态中的值，有什么区别
//					eqLogUserStatus.setSumWater(new BigDecimal(cardObj.getInteger("sumWater")));
//				}
//				if (cardObj.containsKey("sumEle")) {
//					//跟设备状态中的值，有什么区别
//					eqLogUserStatus.setSumEle(new BigDecimal(cardObj.getInteger("sumEle")));
//				}
//				if (cardObj.containsKey("time")) {
//					eqLogUserStatus.setSettleStatus(String.valueOf(cardObj.getInteger("time")));
//				}
//			}
//		}
//	}

}
